<template>
  <div v-show="!isDetail" class="h-full px-4 py-3 rounded-lg">
    <div class="flex justify-between">
      <a-button class="ghost" @click="openModal('add')"> 添加NVR设备 </a-button>
      <div class="flex justify-end">
        <a-input
          v-model:value.trim="searchForm.name"
          placeholder="请输入NVR名称"
          v-filter-special-chars
          maxLength="20"
          class="_input mr-5"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.status"
          allow-clear
          placeholder="选择NVR状态"
          :options="statusList"
        />
        <SerchButtom @search="getList(true, true)" @reset="reset" />
      </div>
    </div>

    <div class="mt-3 h-[calc(100%-6rem)] overflow-y-auto">
      <a-table
        :dataSource="dataSource"
        :pagination="false"
        :columns="columns"
        :loading="loading"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'order'">
            {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
          </template>
          <template v-else-if="column.dataIndex === 'camera'">
            <a-button type="link" @click="onPreview(record)">详情</a-button>
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <a-button type="link" @click="openModal('active', record)">
              激活
            </a-button>
            <a-button
              type="link"
              class="delete"
              @click="openModal('delete', record)"
            >
              删除
            </a-button>
            <a-button type="link" @click="openModal('edit', record)">
              修改
            </a-button>
          </template>
        </template>
      </a-table>
    </div>

    <div class="mt-3 h-8">
      <Pagination @getListByPage="getListByPage" ref="paginationRef" />
    </div>
  </div>

  <div v-show="isDetail">
    <IndexDetail ref="indexDetailRef" @change="handleChangeDetail" />
  </div>

  <IndexModal
    ref="indexModalRef"
    :selectList="manuList"
    @isOk="getList(true, true)"
  />
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { getDict } from '@/api/dict'

import IndexModal from './IndexModal.vue'
import IndexDetail from './IndexDetail.vue'
import SerchButtom from '@/components/YuanQi-SearchButtom.vue'

import { list } from './serve.api'

const dataSource = ref()
const loading = ref<boolean>(false)
const paginationRef = ref()
const statusList = ref<any[]>([])
const manuList = ref<any[]>([])

const indexModalRef = ref()
const indexDetailRef = ref()
const isDetail = ref<boolean>(false)

let searchFormClone = {
  pageNum: 1,
  pageSize: 10,
  name: '',
  status: null
}

// 搜索参数
const searchForm = reactive({
  name: '',
  status: null
})

const columns = ref([
  {
    title: '序号',
    dataIndex: 'order',
    width: '7.5rem'
  },
  {
    title: 'NVR名称',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true
  },
  {
    title: 'NVR厂商',
    dataIndex: 'manufacturerName',
    key: 'manufacturerName',
    ellipsis: true
  },
  {
    title: 'IP地址',
    dataIndex: 'ip',
    key: 'ip',
    ellipsis: true
  },
  {
    title: '端口',
    dataIndex: 'port',
    key: 'port',
    ellipsis: true
  },
  {
    title: 'NVR状态',
    dataIndex: 'statusName',
    key: 'statusName',
    ellipsis: true
  },
  {
    title: '摄像机',
    dataIndex: 'camera',
    key: 'camera',
    ellipsis: true
  },
  {
    title: '操作',
    dataIndex: 'operation',
    key: 'operation',
    ellipsis: true,
    width: '12rem'
  }
])

// 重置
const reset = () => {
  searchForm.name = ''
  searchForm.status = null
  getList(true, true)
}

const pagination = reactive({
  total: 0,
  pageSize: 10, // 每页中显示10条数据
  position: ['bottomCenter'],
  current: 1,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条`
})

// 初始化数据
const getList = (flag = false, isSearch = false) => {
  if (flag) {
    pagination.current = 1
    paginationRef.value.changeCurrent(pagination.current)
  }
  loading.value = true
  dataSource.value = []

  const params = {
    name: searchForm.name,
    status: searchForm.status,
    pageNum: pagination.current,
    pageSize: pagination.pageSize
  }
  if (isSearch) {
    searchFormClone = JSON.parse(JSON.stringify(params))
  } else {
    params.name = searchFormClone.name
    params.status = searchFormClone.status
  }

  list(params)
    .then((res) => {
      if (res) {
        console.log(res, '视频装置管理列表')

        dataSource.value = res.list
        pagination.total = res.total

        paginationRef.value.changeTotal(pagination.total)
        loading.value = false
      } else {
        loading.value = false
      }
    })
    .catch(() => {
      loading.value = false
    })
}

const getListByPage = (page: number, pageSize: number) => {
  if (pagination.current !== page) {
    pagination.current = page
    getList()
  }
  if (pagination.pageSize !== pageSize) {
    pagination.pageSize = pageSize
    getList(true)
  }
}

// 新增/编辑/删除/激活操作
type ModalType = 'add' | 'edit' | 'delete' | 'active'

const openModal = (type: ModalType, data?: any) => {
  indexModalRef.value.showModal(type, data)
}

const handleChangeDetail = () => (isDetail.value = !isDetail.value)
function onPreview(item: any) {
  handleChangeDetail()
  nextTick(() => {
    indexDetailRef.value.transferData(item)
  })
}

onMounted(() => {
  getDict('nvr_camera_status').then((res) => {
    statusList.value = res.map((item: any) => ({
      label: item.dictLabel,
      value: item.dictValue
    }))
  })

  getDict('nvr_manufacturer').then((res) => {
    manuList.value = res.map((item: any) => ({
      label: item.dictLabel,
      value: item.dictValue
    }))
  })

  getList(true, true)
})
</script>
<style lang="less" scoped>
._input {
  width: 13.125rem;
  vertical-align: middle;
}
</style>
